package com.example.toolsdemo.controller;

import com.example.toolsdemo.common.ServerResponse;
import com.example.toolsdemo.dataobject.Orders;
import com.example.toolsdemo.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author danyubin
 */
@Api(value = "订单信息", tags = {"订单信息的相关接口"})
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "根据订单id查询", notes = "根据订单id查询", httpMethod = "GET")
    @GetMapping("/get")
    public ServerResponse<Orders> get(@RequestParam("id") String id) {
        Orders order = orderService.getOrder(id);
        return ServerResponse.success(order);
    }

    @ApiOperation(value = "保存订单", notes = "保存订单", httpMethod = "POST")
    @PostMapping("/save")
    public ServerResponse save(@RequestBody Orders order) {
        orderService.saveOrder(order);
        return ServerResponse.success("保存成功");
    }

    @ApiOperation(value = "修改订单", notes = "修改订单", httpMethod = "POST")
    @PostMapping("/modify")
    public ServerResponse modify(@RequestBody Orders order) {

        return orderService.modifyOrder(order);
    }

    @ApiOperation(value = "删除订单byid", notes = "删除订单byid", httpMethod = "POST")
    @PostMapping("/delete")
    public ServerResponse delete(String id) {
        Integer integer = orderService.deleteOrder(id);
        return ServerResponse.success();
    }

    @ApiOperation(value = "查询订单列表byUserId分页", notes = "查询订单列表byUserId分页", httpMethod = "GET")
    @GetMapping("/getOrdersByUserId")
    public ServerResponse getOrdersByUserId(@ApiParam(name = "userId", value = "用户id", required = true)
                                                  @RequestParam("userId") String userId,
                                              @ApiParam(name = "pageNum", value = "页号", required = false)
                                              @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                              @ApiParam(name = "pageSize", value = "每页大小", required = false)
                                              @RequestParam(value = "pageSize", defaultValue = "1", required = false) Integer pageSize) {

        return orderService.getOrdersByUserId(userId, pageNum, pageSize);
    }
}
